package com.homestay.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class CookieUtils {
    public static String getCookieValue(HttpServletRequest request, String cookieName) {
        try {
            if (request.getCookies() != null && request.getCookies().length > 0) {
                for (Cookie cookie : request.getCookies()) {
                    if (cookie.getName().equals(cookieName)) {
                        return URLDecoder.decode(cookie.getValue(), "UTF-8");
                    }
                }
            }
            return "";
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}
